import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_base_kit/common/loading_widget.dart';
import 'package:flutter_base_kit/net/dio_utils.dart';
import 'package:flutter_base_kit/utils/log_util.dart';
import 'package:get/get.dart';

import 'controller/splash_controller.dart';

/// 创建人： Created by zhaolong
/// 创建时间：Created by  on 2020/7/20.
///
/// 创建人： Created by zhaolong
/// 创建时间：Created by  on 2020/9/25.
///
/// gongzhonghao biglead
/// gongzhonghao biglead
/// github https://github.com/zhaolongs
/// bili https://space.bilibili.com/513480210
/// zhihu https://www.zhihu.com/people/zhao-long-90-89
/// csdn https://blog.csdn.net/zl18603543572
/// 西瓜视频 https://www.ixigua.com/home/3662978423/// lib/src/splash_page.dart
/// 第一次 新手引导页面
class SplashPage extends StatelessWidget {
  final SplashController _controller = Get.put(SplashController());

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      //填充布局
      body: SizedBox(
        width: double.infinity,
        height: double.infinity,
        child: GetBuilder(
          builder: (SplashController controller) {
            if (controller.loadingStatues == LoadingStatues.loading) {
              return LoadingWidget();
            }
            return Stack(
              alignment: Alignment.center,
              children: [
                //底部可滑动的图片
                buildPageView(),
                //左下角显示的进入首页面按钮
                Positioned(
                  bottom: 40,
                  left: 20,
                  child: buildBottomButton(),
                ),
              ],
            );
          },
        ),
      ),
    );
  }

  ///代码清单11-7 底部可滑动的图片
  ///lib/splash_page.dart
  Widget buildPageView() {
    //PageView用于整屏切换效果
    //默认情况下是左右切换
    return PageView.builder(
      //滑动视频滑动结束时回调
      //参数 value PageView当前显示的页面索引
      onPageChanged: (value) {
        LogUtil.e("pageView on changed $value");
        _controller.onPageChanged(value);
      },
      //构建条目的总个数，如这里的4
      itemCount: _controller.imageList.length,
      //每一页的显示Widget
      itemBuilder: (BuildContext context, int postion) {
        //这里直接使用的是本地资源目录下的图片
        return Center(
          child: Image.asset(
            _controller.imageList[postion],
            fit: BoxFit.fitHeight,
          ),
        );
      },
    );
  }

  Widget buildBottomButton() {
    if (!_controller.showNext) {
      return Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          //显示的页面角标
          Text(
            _controller.indexStr,
            maxLines: 1,
            overflow: TextOverflow.clip,
            style: TextStyle(fontSize: 16, color: Colors.indigo),
          ),
        ],
      );
    }
    return InkWell(
      onTap: () {
        _controller.goHome();
      },
      child: Container(
        //内边距
        padding: EdgeInsets.only(left: 10, right: 10, top: 4, bottom: 4),
        alignment: Alignment.center,
        //圆角边框
        decoration: BoxDecoration(
            borderRadius: BorderRadius.all(Radius.circular(20)),
            border: Border.all(
              width: 3,
              color: Colors.indigo,
            )),
        child: Text(
          "去首页",
          maxLines: 1,
          overflow: TextOverflow.clip,
        ),
      ),
    );
  }
}
